package cn.ronghuanet.org.service.impl;

import cn.ronghuanet.base.service.impl.BaseServiceImpl;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.mapper.EmployeeMapper;
import cn.ronghuanet.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation=Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    //创建redis模板对象
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional //增删改都需要事务
    public void insertOne(Employee employee) {
        //第一次删除
        redisTemplate.delete("employeeList");
        employeeMapper.insertOne(employee);
        try {
            //停留一段时间
            Thread.sleep(2000);
            //第二次删除
            redisTemplate.delete("employeeList");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional //增删改都需要事务
    public void updateOne(Employee employee) {
        //第一次删除
        redisTemplate.delete("employeeList");
        employeeMapper.updateOne(employee);
        try {
            //停留一段时间
            Thread.sleep(2000);
            //第二次删除
            redisTemplate.delete("employeeList");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional //增删改都需要事务
    public void deleteOne(Long id) {
        //第一次删除
        redisTemplate.delete("employeeList");
        employeeMapper.deleteOne(id);
        try {
            //停留一段时间
            Thread.sleep(2000);
            //第二次删除
            redisTemplate.delete("employeeList");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional //增删改都需要事务
    public void patchDel(List<Long> ids) {
        //第一次删除
        redisTemplate.delete("employeeList");
        employeeMapper.patchDel(ids);
        try {
            //停留一段时间
            Thread.sleep(2000);
            //第二次删除
            redisTemplate.delete("employeeList");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Employee> loadAll() {
        //缓存方案,曹勇分布式缓存,redis
        Object employeeList=redisTemplate.opsForValue().get("employeeList");
        //缓存没查到
        if (employeeList==null){
            //双写一致性,redis查询没有值,就在数据库查
            List<Employee> list = employeeMapper.loadAll();
            //同步缓存到Redis
            redisTemplate.opsForValue().set("employeeList",list);
            return list;
        }
        return (List<Employee>)employeeList;
    }


}
